package com.laoguo.config;

import com.alibaba.druid.pool.DruidDataSource;
import com.laoguo.properties.DruidProperties;
import com.laoguo.properties.MutiDataSourceProperties;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.sql.DataSource;
import java.sql.SQLException;
import java.util.HashMap;

@Configuration
@EnableTransactionManagement(order = 2)
@MapperScan(basePackages = {"com.laoguo.dao"})
public class MybatisPlusConfig {
    @Autowired
    DruidProperties druidProperties;
    @Autowired
    MutiDataSourceProperties mutiDataSourceProperties;

    /**
     * 核心数据源
     */
    public DruidDataSource coreDataSource() {
        DruidDataSource dataSource = new DruidDataSource();
        druidProperties.config(dataSource);
        return dataSource;
    }

    /**
     * 另一个数据源
     */
    public DruidDataSource bizDataSource() {
        DruidDataSource dataSource = new DruidDataSource();
//        druidProperties.config(dataSource);
        mutiDataSourceProperties.config(dataSource);
        return dataSource;
    }

    /**
     * 单数据源连接池配置
     */
    @Bean
    @ConditionalOnProperty(prefix = "guojunjie",name = "muti-datasource-open",havingValue = "false")
    public DruidDataSource singleDatasource() {
        return coreDataSource();
    }

    /**
     * 多数据源连接池配置
     */
    @ConditionalOnProperty(prefix = "guojunjie",name = "muti-datasource-open",havingValue = "true")
    @Bean(name = "mutiDataSource")
    public DynamicDataSource mutiDataSource() {
        DruidDataSource coreDataSource = coreDataSource();
        DruidDataSource bizDataSource = bizDataSource();
        try {
            coreDataSource.init();
            bizDataSource.init();
        } catch (SQLException sql) {
            sql.printStackTrace();
        }

        DynamicDataSource dynamicDataSource = new DynamicDataSource();
        HashMap<Object, Object> hashMap = new HashMap<>();
        hashMap.put(CustomerContextHolder.DATA_SOURCE_MASTER, coreDataSource);
        hashMap.put(CustomerContextHolder.DATA_SOURCE_SLAVE, bizDataSource);
        dynamicDataSource.setTargetDataSources(hashMap);
        dynamicDataSource.setDefaultTargetDataSource(coreDataSource);
        return dynamicDataSource;
    }

/**
 * 以下配置在mybatis-plus下不起作用。这个是mybatis中用
 */
//    @Bean
//    public SqlSessionFactory sqlSessionFactory(
//            @Qualifier("mutiDataSource") DataSource dynamicDataSource)
//            throws Exception {
//        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
//        bean.setDataSource(dynamicDataSource);
////        bean.setMapperLocations(mybatisProperties.resolveMapperLocations());
////        bean.setTypeAliasesPackage(mybatisProperties.getTypeAliasesPackage());
////        bean.setConfiguration(mybatisProperties.getConfiguration());
//        return bean.getObject();
//    }
//    @Bean(name = "sqlSessionTemplate")
//    public SqlSessionTemplate sqlSessionTemplate(@Qualifier("sqlSessionFactory") SqlSessionFactory sqlSessionFactory)
//            throws Exception {
//        return new SqlSessionTemplate(sqlSessionFactory);
//    }

    /**
     * 将动态数据源添加到事务管理器中，并生成新的bean
     * @return the platform transaction manager
     */
//    @Bean
//    public PlatformTransactionManager transactionManager() {
//        return new DataSourceTransactionManager(mutiDataSource());
//    }

}
